<?php
namespace froyo\cherry\Base;

use froyo\cherry\Exception\HttpException;

class Cherry extends BaseObject{
	public static $classMap = [];
	public static $app;
	public static $langmaps = [];

	public static function setApp($app){
		Cherry::$app = $app;
	}

	public static function register(){
		spl_autoload_register(['froyo\\cherry\\Base\\Cherry','autoload']);
		//注册错误处理函数
		set_exception_handler(['froyo\\cherry\\Base\\Cherry','errorhandler']);
	}

	public static function lang($str,$params=[],$module = ''){
		return Cherry::$app->getLangFactory()->lang($str,$params,$module);
		
	}

	public static function autoload($className){
		if (isset(static::$classMap[$className])) {
            $classFile = static::$classMap[$className];
            
        } elseif (strpos($className, '\\') !== false) {
            $classFile = str_replace('\\', '/', CHERRY_APP_ROOT.$className) . '.php';
            if ($classFile === false || !is_file($classFile)) {
                return;
            }
        } else {
            return;
        }
        include $classFile;
	}

	public static function errorhandler($ex ){
		// var_dump(expression)
		$response = Cherry::$app->getResponse();
		if($ex instanceof HttpException)
		{
			$response->setHttpCode($ex->getHttpCode());
			$response->setData($ex->getMessage());
			
		}
		else
		{
			$response->setHttpCode(500);
			$response->setData($ex->getMessage());
			
		}
		$response->send();
	}
}

